package com.egghiro.framework.j2se.game.core.view;

import java.awt.DisplayMode;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

public class ScreenManager {

    public static final GraphicsEnvironment environment = GraphicsEnvironment
            .getLocalGraphicsEnvironment();

    public static final GraphicsDevice graphicsDevice = environment
            .getDefaultScreenDevice();

    public static final GraphicsConfiguration graphicsConfiguration = graphicsDevice
            .getDefaultConfiguration();

    /**
     * 查询可用的屏幕设备
     * 
     * @param width
     * @param height
     * @return
     */
    public final static DisplayMode searchFullScreenModeDisplay(int width,
            int height) {
        return searchFullScreenModeDisplay(graphicsDevice, width, height);
    }

    /**
     * 查询可用的屏幕设备
     * 
     * @param device
     * @param width
     * @param height
     * @return
     */
    public final static DisplayMode searchFullScreenModeDisplay(
            GraphicsDevice device, int width, int height) {
        DisplayMode displayModes[] = device.getDisplayModes();
        int currentDisplayPoint = 0;
        DisplayMode fullScreenMode = null;
        DisplayMode normalMode = device.getDisplayMode();
        DisplayMode adisplaymode[] = displayModes;
        int i = 0, length = adisplaymode.length;
        for (int j = length; i < j; i++) {
            DisplayMode mode = adisplaymode[i];
            if (mode.getWidth() == width && mode.getHeight() == height) {
                int point = 0;
                if (normalMode.getBitDepth() == mode.getBitDepth()) {
                    point += 40;
                } else {
                    point += mode.getBitDepth();
                }
                if (normalMode.getRefreshRate() == mode.getRefreshRate()) {
                    point += 5;
                }
                if (currentDisplayPoint < point) {
                    fullScreenMode = mode;
                    currentDisplayPoint = point;
                }
            }
        }
        return fullScreenMode;
    }

}
